const { ADD_TODO, TOGGLE_TODO } = require("../actions");

const todos = (state = [], action) => {
    switch (action.type) {
        case ADD_TODO:
            return [
                ...state,
                {
                    id: action.id,
                    text: action.text,
                    completed: false
                }
            ];
        case TOGGLE_TODO:
            return state.map((todo, index) => {
                if (action.id === index) {
                    return Object.assign({}, todo, {
                        completed: !action.completed
                    });
                }else{
                    return todo;
                }
            });
        
        default:
            return state;
    }
}

export default todos;